/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "spatial_awareness_mgr_stub.h"

#include <message_parcel.h>
#include <string_ex.h>
#include "spatial_awareness_common.h"

using namespace OHOS::HiviewDFX;

namespace OHOS {
namespace Msdp {
int SpatialaWarenessMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
    MessageParcel &reply, MessageOption &option)
{
    SPATIAL_LOGI(MODULE_SERVICE,
        "SpatialaWarenessMgrStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
    std::u16string descripter = SpatialaWarenessMgrStub::GetDescriptor();
    std::u16string remoteDescripter = data.ReadInterfaceToken();
    if (descripter != remoteDescripter) {
        SPATIAL_LOGE(MODULE_SERVICE,
            "SpatialaWarenessMgrStub::OnRemoteRequest failed, descriptor is not matched!");
        return E_GET_SPATIAL_AWARENESS_SERVICE_FAILED;
    }
    switch (code) {
        case static_cast<int>(ISpatialAwarenessMgr::REG_SPATIAL_LOCATION_CALLBACK): {
            return SubscribeSpatialLocationRelationshipStub(data);
        }
        case static_cast<int>(ISpatialAwarenessMgr::UNREG_SPATIAL_LOCATION_CALLBACK): {
            return UnSubscribeSpatialLocationRelationshipStub(data);
        }
        default: {
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
        }
    }
    return ERR_OK;
}

int32_t SpatialaWarenessMgrStub::SubscribeSpatialLocationRelationshipStub(MessageParcel& data)
{
    SPATIAL_LOGI(MODULE_SERVICE, "%{public}s enter", __func__);
    int32_t mode = 0;
    READ_PARCEL_WITH_RET(data, Int32, mode, E_READ_PARCEL_ERROR);
    std::unique_ptr<SpatialDeviceInfo> info(data.ReadParcelable<SpatialDeviceInfo>());
    RETURN_IF_WITH_RET((info == nullptr), E_READ_PARCEL_ERROR);
    sptr<IRemoteObject> obj = data.ReadRemoteObject();
    RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
    sptr<ISpatialLocationCallback> callback = iface_cast<ISpatialLocationCallback>(obj);
    RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);

    SubscribeSpatialLocationRelationship(static_cast<SpatialLocationMode>(mode), *info, callback);
    return ERR_OK;
    SPATIAL_LOGI(MODULE_SERVICE, "%{public}s return", __func__);
}

int32_t SpatialaWarenessMgrStub::UnSubscribeSpatialLocationRelationshipStub(MessageParcel& data)
{
    SPATIAL_LOGI(MODULE_SERVICE, "%{public}s enter", __func__);
    int32_t mode = 0;
    READ_PARCEL_WITH_RET(data, Int32, mode, E_READ_PARCEL_ERROR);
    std::unique_ptr<SpatialDeviceInfo> info(data.ReadParcelable<SpatialDeviceInfo>());
    sptr<IRemoteObject> obj = data.ReadRemoteObject();
    RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
    sptr<ISpatialLocationCallback> callback = iface_cast<ISpatialLocationCallback>(obj);
    RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
    UnSubscribeSpatialLocationRelationship(static_cast<SpatialLocationMode>(mode), *info, callback);
    return ERR_OK;
    SPATIAL_LOGI(MODULE_SERVICE, "%{public}s return", __func__);
}
} // namespace Msdp
} // namespace OHOS